package com.water.devtool.extend;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.water.devtool.define.EntityBean;
import com.water.devtool.ui.UiUtil;

public class JspComposite extends Composite {
	private EntityBean entity;
	private JspConfig config;
	private Text text;
	private Text text_1;
	private Text text_3;
	private Table table;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public JspComposite(Composite parent, int style) {
		super(parent, style);

		Label label = new Label(this, SWT.NONE);
		label.setText("查询字段：");
		label.setAlignment(SWT.RIGHT);
		label.setBounds(46, 45, 98, 20);

		text = new Text(this, SWT.BORDER);
		text.setBounds(149, 42, 872, 26);

		Label label_1 = new Label(this, SWT.NONE);
		label_1.setText("显示字段：");
		label_1.setAlignment(SWT.RIGHT);
		label_1.setBounds(46, 108, 98, 20);

		text_1 = new Text(this, SWT.BORDER);
		text_1.setBounds(149, 105, 872, 26);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.setBounds(1053, 40, 98, 30);
		btnNewButton.setText("选择字段");

		Button button = new Button(this, SWT.NONE);
		button.setText("选择字段");
		button.setBounds(1053, 103, 98, 30);

		Label label_3 = new Label(this, SWT.NONE);
		label_3.setText("分页行数：");
		label_3.setAlignment(SWT.RIGHT);
		label_3.setBounds(177, 156, 98, 20);

		text_3 = new Text(this, SWT.BORDER);
		text_3.setBounds(290, 150, 349, 26);

		Button button_1 = new Button(this, SWT.CHECK);
		button_1.setText("是否分页");
		button_1.setBounds(53, 156, 91, 20);

		createTable();

		Button button_2 = new Button(this, SWT.NONE);
		button_2.setText("确定");
		button_2.setBounds(503, 592, 98, 30);

		Button button_3 = new Button(this, SWT.NONE);
		button_3.setText("关闭");
		button_3.setBounds(716, 592, 98, 30);

	}

	// 创建表格
	private void createTable() {
		// 表格布局
		GridData gridData = new org.eclipse.swt.layout.GridData();
		gridData.horizontalAlignment = SWT.LEFT;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = SWT.FILL;

		// 创建表格，使用SWT.FULL_SELECTION样式，可同时选中一行
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(55, 247, 968, 268);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		// 创建表头的字符串数组
		String[] tableHeader = new String[] { "图标", "函数", "颜色", "参数", "是否刷新", "是否确认" };
		int[] widths = { 100, 100, 100, 100, 100, 100, 100 };
		for (int i = 0; i < tableHeader.length; i++) {
			TableColumn tableColumn = new TableColumn(table, SWT.NONE);
			tableColumn.setText(tableHeader[i]);
			// 设置表头可移动，默认为false
			tableColumn.setMoveable(false);
			tableColumn.setWidth(widths[i]);
		}
		loadTableData();

		UiUtil.setTableEditor(table, tableHeader);

		// ******************************************************/
	}

	protected void loadTableData() {
		table.removeAll();

		List<JspActionInfo> actions = config.getActions();
		if (actions == null) {
			return;
		}
		for (JspActionInfo a : actions) {
			TableItem item = new TableItem(table, SWT.NONE);
			String[] param = new String[] { a.getIcon(), a.getFunction(), a.getColor(), a.getParams(),
					String.valueOf(a.isRefreshTable()), String.valueOf(a.isNeedConfirm()) };
			item.setText(param);
		}

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void setEntity(EntityBean entity) {
		this.entity = entity;
		Object obj = entity.getLevelConfig("jsp");
		if (obj == null) {
			obj = new JspConfig();
			entity.addLevelConfig("jsp", obj);
		}
		config = (JspConfig) obj;
	}
}
